package mygame;

import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.*;
import com.jme3.math.Vector3f;

/**
 *
 * @author Xinchro
 */
public class Input implements AnalogListener, ActionListener{
    private InputManager inMan;
    private Main main;
    
    Vector3f camDir;
    Vector3f camLeft;
    
    public Input(InputManager inputManager, Main main){
        System.out.println("Hello? Yes, this is input.");
	this.inMan = inputManager;
	this.main = main;
	setupMappings();
	setupListener();
	
    }
    
    private void setupMappings(){
	inMan.clearMappings();
        inMan.addMapping("up", new KeyTrigger(KeyInput.KEY_W));
        inMan.addMapping("down", new KeyTrigger(KeyInput.KEY_S));
        inMan.addMapping("left", new KeyTrigger(KeyInput.KEY_A));
        inMan.addMapping("right", new KeyTrigger(KeyInput.KEY_D));
	
	inMan.addMapping("SS", new KeyTrigger(KeyInput.KEY_C));
	
	inMan.addMapping("restart", new KeyTrigger(KeyInput.KEY_R));
	
	inMan.addMapping("swapprev", new KeyTrigger(KeyInput.KEY_Q));
	inMan.addMapping("swapnext", new KeyTrigger(KeyInput.KEY_E));
	
        inMan.addMapping("mouseLdown", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
	inMan.addMapping("mouseRdown", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
	inMan.addMapping("zoomout", new MouseAxisTrigger(MouseInput.AXIS_WHEEL,false));
	inMan.addMapping("zoomin", new MouseAxisTrigger(MouseInput.AXIS_WHEEL,true));
			
	inMan.addMapping("quit", new KeyTrigger(KeyInput.KEY_ESCAPE));
    }
    
    private void setupListener(){
	inMan.addListener(this, "up");
        inMan.addListener(this, "down");
	inMan.addListener(this, "left");
	inMan.addListener(this, "right");
	
	inMan.addListener(this, "SS");
	
	inMan.addListener(this, "restart");
	
	inMan.addListener(this, "swapprev");
	inMan.addListener(this, "swapnext");
	
	inMan.addListener(this, "mouseLdown");
	inMan.addListener(this, "mouseRdown");
	inMan.addListener(this, "zoomin");
	inMan.addListener(this, "zoomout");
	
	inMan.addListener(this, "quit");
    }
    
    float tempLoc[] = {0.0f,0.0f,0.0f};
    float tempSp = 0.05f;
    
    /*
     * Used for continuous actions
     */
    public void onAnalog(String name, float value, float tpf){
        this.camDir = main.getCamera().getDirection().clone().multLocal(0.1f);
	this.camLeft = main.getCamera().getLeft().clone().multLocal(0.1f);
	camDir.y = 0;
        camLeft.y = 0;
        switch(name.toLowerCase()){
            case "up":
		//System.out.println("forward pressed");
		//main.playerGeom.setLocalTranslation(main.playerGeom.getWorldTranslation().add(camDir));
		main.playModel.setLocalTranslation(main.playModel.getWorldTranslation().add(camDir));
		
		break;
	    case "down":
//		main.playerGeom.setLocalTranslation(main.playerGeom.getWorldTranslation().add(camDir.negate()));
		main.playModel.setLocalTranslation(main.playModel.getWorldTranslation().add(camDir.negate()));
		//System.out.println("backward pressed");
		break;
	    case "left":
//		main.playerGeom.setLocalTranslation(main.playerGeom.getWorldTranslation().add(camLeft));
		main.playModel.setLocalTranslation(main.playModel.getWorldTranslation().add(camLeft));
//		System.out.println("left pressed");
		break;
	    case "right":
//		main.playerGeom.setLocalTranslation(main.playerGeom.getWorldTranslation().add(camLeft.negate()));
		main.playModel.setLocalTranslation(main.playModel.getWorldTranslation().add(camLeft.negate()));
//		System.out.println("right pressed");
		break;
		
	    
        }
	//main.b.center.set(new Vector3f(tempLoc[0],tempLoc[1],tempLoc[2]));
	//main.geom.setLocalTranslation(new Vector3f(tempLoc[0],tempLoc[1],tempLoc[2]));
	//main.geom.center().move(new Vector3f(tempLoc[0],tempLoc[1],tempLoc[2]));
	//main.getCamera().lookAt(main.geom.getWorldTranslation(), new Vector3f(0.0f, 0.0f, 1.0f));
	//System.out.println(main.geom.getWorldTranslation());
//	System.out.print(tempLoc[0]);
//	System.out.print(" ");
//	System.out.print(tempLoc[1]);
//	System.out.print(" ");
//	System.out.println(tempLoc[2]);
    }
    
    /*
     * Used for single is pressed/is not pressed actions
     */    
    public void onAction(String name, boolean press, float tpf){
        switch(name.toLowerCase()){
            case "up":
//		System.out.println("forward pressed");
		break;
	    case "down":
//		System.out.println("backward pressed");
		break;
	    case "left":
//		System.out.println("left pressed");
		break;
	    case "right":
//		System.out.println("right pressed");
		break;
	    case "zoomin":
		main.camZoom -= 0.5f;
//		System.out.println("zoomin pressed");
		break;
	    case "zoomout":
		main.camZoom += 0.5f;
//		System.out.println("zoomout pressed");
		break;
	    case "swapnext":
		//swap to next pet
		if(press){
		    main.removePets();
		    main.player.swapNextPet();
		    main.putPet();
		    main.petEle.setText(main.player.getCurPet().getType().getElement());
		}
		break;
	    case "swapprev":
		//swap to previous pet
		if(press){
		    main.removePets();
		    main.player.swapPrevPet();
		    main.putPet();
		    main.petEle.setText(main.player.getCurPet().getType().getElement());
		}
		break;
		
	    case "mouseldown":
		if(press){
		    if(main.petsHitting){
			for(int i=0; i<main.targetEnemies.size(); i++){
			   // System.out.println(main.targetEnemies.get(i).getHealth());
			    main.getPlayer().getCurPet().attack(main.targetEnemies.get(i));
			    System.out.println(main.targetEnemies.get(i).getHealth());
			}
		    }
		}
		break;
	    case "ss":
		main.takeScreenShot();
		break;
	    case "mouserdown":
		if(press){
		    main.resetPet();
		}
		break;
	    case "restart":
		//welp
		break;
	    case "quit":
		main.quit();
//		System.out.println("quit pressed");
		break;
        }
	//System.out.println(main.camZoom);
	//System.out.println(main.getCamera().getLocation());
    }        
}
